package com.lucenly.spell.base

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.ImmersionBar
import com.lucenly.spell.R
import com.lucenly.spell.databinding.ActivityBaseBinding
import com.lucenly.spell.utils.AppManager
import com.lucenly.spell.utils.ext_toast
import com.moregold.mvvmlib.base.BaseVMActivity
import com.moregold.mvvmlib.ext.ext_doubleClick
import gorden.rxbus2.RxBus

/**
 *  Created by Yang on 2020/12/14 0014 17:40.
 *
 */
abstract class BaseActivity<VB : ViewBinding> : BaseVMActivity() {
    lateinit var baseBinding: ActivityBaseBinding
    lateinit var binding: VB
//    protected val binding: VB by lazy {
//        //使用反射得到viewbinding的class
//        val type = javaClass.genericSuperclass as ParameterizedType
//        val aClass = type.actualTypeArguments[0] as Class<*>
//        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
//        method.invoke(null, layoutInflater) as VB
//    }

    protected abstract fun getViewBinding(): VB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(binding.root)
        baseBinding = ActivityBaseBinding.inflate(layoutInflater)
        setContentView(baseBinding.root)
        binding = getViewBinding()
        when(getStatusType()){
            0->{
                ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.transparent)
                    .titleBar(baseBinding.clLayoutToolbar).init()
                baseBinding.clLayoutToolbar.setBackgroundColor(Color.parseColor("#00000000"))
                hideTitleBar()
            }
            1->{ ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.transparent)
                .titleBar(baseBinding.clLayoutToolbar).init()
                baseBinding.contentMain.setBackgroundColor(Color.parseColor("#f5f5f5"))
            }
            2->{
                ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white)
                    .titleBar(baseBinding.clLayoutToolbar).init()
                baseBinding.clLayoutToolbar.setBackgroundColor(Color.parseColor("#ffffff"))
                baseBinding.ivToolbarBack.setImageResource(R.mipmap.back_b)
                baseBinding.tvToolbarTitle.setTextColor(Color.parseColor("#000000"))
                baseBinding.tvToolbarRight.setTextColor(Color.parseColor("#000000"))
                baseBinding.contentMain.setBackgroundColor(Color.parseColor("#f5f5f5"))
            }
            3->{
                ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.transparent)
                    .titleBar(baseBinding.clLayoutToolbar).init()
                baseBinding.contentMain.setBackgroundColor(Color.parseColor("#00000000"))
                baseBinding.clLayoutToolbar.setBackgroundColor(Color.parseColor("#00000000"))
            }
        }


        // 添加Activity到堆栈
        AppManager.appManager?.addActivity(this)
        //初始化标题栏
        initView()
        RxBus.get().register(this)

        startObserve()
        initData()
        initEvent()
    }

    private fun initView() {
        baseBinding?.tvToolbarTitle.text = setTitle()
        baseBinding?.ivToolbarBack.ext_doubleClick {
            finish()
        }
    }
    open fun getStatusType():Int{
        return 1 //0透明 1 彩色 2是白色
    }
    abstract fun setTitle(): String

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.get().unRegister(this)
        AppManager.appManager?.finishActivity(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
        if (menuItem.itemId == R.id.home) {
            onBackPressed()
        }
        return super.onOptionsItemSelected(menuItem)
    }



    /**
     * 隐藏标题栏
     */
    protected fun hideTitleBar() {
        baseBinding?.clLayoutToolbar.visibility = View.GONE
    }


    /**
     * 设置右边图标
     */
    protected fun setRightImg(id: Int) {
        baseBinding?.ivToolbarRight.setImageResource(id)
        baseBinding?.ivToolbarRight.visibility = View.VISIBLE

    }

    /**
     * 设置中间图片
     */
    protected fun setCenterImg(id: Int) {
        baseBinding?.ivToolbarCenter.setImageResource(id)
        baseBinding?.ivToolbarCenter.visibility = View.VISIBLE
    }

    /**
     * 获取右边图标
     */
    protected fun getRightImg(): ImageView {
        return baseBinding?.ivToolbarRight
    }

    /**
     * 设置右边图标点击事件
     */
    protected fun setRightImgClick(click: () -> Unit){
        baseBinding?.ivToolbarRight.ext_doubleClick {
            click.invoke()
        }
    }

    protected fun setRightText(text:String){
        baseBinding.tvToolbarRight.text = text
        baseBinding.tvToolbarRight.visibility = View.VISIBLE
    }
    protected fun setRightTextClick(click: () -> Unit){
        baseBinding?.tvToolbarRight.ext_doubleClick {
            click.invoke()
        }
    }
}